package test.upload;

//import java.util.Date;
//import java.sql.Date;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Disanti {
    public static void main(String[] args) {
        try {
            ServerSocket s = new ServerSocket(9999);
            while (true){
                Socket ac = s.accept();

               // String s1 = String.valueOf(Math.random());
                new FileReceive(ac).start();

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
class FileReceive extends Thread{
    private final Socket s;
    //private final String name;

    public FileReceive( Socket s) {
        this.s=s;
        //this.name=name;
    }

    @Override
    public void run() {
        try {
            InputStream inp = s.getInputStream();
            PrintWriter pri = new PrintWriter(s.getOutputStream(),true);//???
            ///?pri.println();
//            if (inp.read()==0){
//                return;
//            }
            BufferedReader buff = new BufferedReader(new InputStreamReader(inp));
            String s = buff.readLine();
            FileOutputStream fil = new FileOutputStream("F:/java82/"+s);
            byte[] bytes = new byte[1024];
            int len=0;
            while ((len=inp.read(bytes))>-1){
                fil.write(bytes,0,len);
            }
            pri.println("接受文件成功");
            fil.close();
            System.out.println("ok,准备退出"+s);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (s!=null){
                    s.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
